package com.htgraph.util;

import com.ht.aems.util.RtDataBuffer;
import com.ht.impl.CustomGraphCompent;
import com.htgraph.flash.FlashThread;
import com.htgraph.flash.Flasher;
import com.htgraph.renderer.CellRenderUpdator;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Wang Jiaxiang
 *         Date: 2010-10-13
 */
public class GraphComponentUtil {
    public static void doCenterLayout(mxGraphComponent graphComponent) {
        mxGraph graph = graphComponent.getGraph();
        double oldScale = graph.getView().getScale();
        //double startX = graph.getView().getGraphBounds().getX() / oldScale;
        //double startY = graph.getView().getGraphBounds().getY() / oldScale;
        double actualGraphWidth = graph.getView().getGraphBounds().getWidth() / oldScale;
        double actualGraphHeight = graph.getView().getGraphBounds().getHeight() / oldScale;
        double width = graphComponent.getWidth();
        double height = graphComponent.getHeight();
        double scale1 = width * 0.9 / actualGraphWidth;
        double scale2 = height * 0.9 / actualGraphHeight;
        double scale = scale1 < scale2 ? scale1 : scale2;
        if (scale < 1e-6) scale = oldScale;
        double offsetX = (width - actualGraphWidth * scale) / 2.0;
        double offsetY = (height - actualGraphHeight * scale) / 2.0;
        if (offsetX < 0) offsetX = 20.0;
        if (offsetY < 0) offsetY = 20.0;
        //System.out.println(graphComponent.getViewport().getX() + "\t" + graphComponent.getViewport().getY());
        //System.out.println("oldScale:" + oldScale +" scale: " + scale);
        //System.out.println(width + "\t" + actualGraphWidth * scale + "\t" + actualGraphWidth + "\t" + startX + "\t" + startY);
        graph.getView().scaleAndTranslate(scale, offsetX / scale, offsetY / scale);
    }

    public static void startUpdateThread(final CustomGraphCompent compent, final Window f, final RtDataBuffer buffer, CellRenderUpdator dataUpdator, Flasher flasher) {
        dataUpdator.setFlasher(flasher);
        buffer.addDataChangeListener(dataUpdator);
        buffer.start();

        final FlashThread t = new FlashThread();
        t.setFlasher(flasher);
        t.start();

        //java.util.Timer tt = new java.util.Timer();
        //tt.schedule(new TimerTask(){
        //    public void run() {
        //        compent.refresh();
        //    }
        //}, 0,1000);
        
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                buffer.setStop(true);
                t.setStop(true);
            }
        });
        f.addWindowStateListener(new WindowStateListener() {
            public void windowStateChanged(WindowEvent e) {
                int state = e.getNewState();
                switch (state) {
                    case JFrame.ICONIFIED:
                        System.out.println("Realtime data query thread is paused.");
                        buffer.setPause(true);
                        t.setPause(true);
                        break;
                    default:
                        System.out.println("Realtime data query thread is consumed.");
                        buffer.setPause(false);
                        t.setPause(false);
                        break;
                }
            }
        });
    }
}
